package com.nineclock.auth.intergration.processor;

import com.nineclock.auth.intergration.entity.IntergrationAuthenticationEntity;
import com.nineclock.common.entity.Result;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.system.dto.SysUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
public class UsernameAndPasswordAuthenticationProcessor implements AuthenticationProcessor{
    @Autowired
    private SysUserFeign sysUserFeign;
    @Override
    public SysUserDTO authenticate(IntergrationAuthenticationEntity entity) {
        String username = entity.getAuthParameter("username");
        String password = entity.getAuthParameter("password");
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        SysUserDTO sysUserDTO = sysUserFeign.queryUser(username).getData();
        if (sysUserDTO == null){
            throw new NcException(ResponseEnum.INVALID_USERNAME_PASSWORD);
        }
        return sysUserDTO;
    }

    @Override
    public boolean support(IntergrationAuthenticationEntity entity) {
        if (entity != null && StringUtils.isEmpty(entity.getAuthType())){
            return true;
        }
        return false;
    }
}
